﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharedLib;
namespace TracyGUI.UIComponent
{
    public class EnhancedTabControl<T> : TabControl where T : TabPage
    {

        public event EventHandler<TabPageEvent> PageRemoved;
        public EnhancedTabControl()
        {
            
        }
        
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            int index = GetTabAtPoint(e.Location);
            if (index >= 0 && e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                DoDragDrop(TabPages[index], DragDropEffects.All);
            }
        }

        private int GetTabAtPoint(Point pt)
        {
            for (int index = 0; index <= TabCount - 1; index++)
            {
                if (GetTabRect(index).Contains(pt.X, pt.Y))
                    return index;
            }
            return -1;
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            if (e.Button == System.Windows.Forms.MouseButtons.Middle)
            {
                int index = GetTabAtPoint(e.Location);
                if (index >= 0)
                {
                    TabPage removedTab = TabPages[index];
                    if (PageRemoved != null) PageRemoved.InvokeEvent(this, new TabPageEvent(removedTab));
                    //if (TabPages[index] == _selectedEntryPage) _selectedEntryPage = null;
                    TabPages.Remove(removedTab);
                }
            }
        }

        protected override void OnDragOver(DragEventArgs e)
        {
            base.OnDragOver(e);
            Point pt = new Point(e.X, e.Y);
            pt = PointToClient(pt);
            int index = GetTabAtPoint(pt);
            if (index >= 0)
            {
                TabPage tabHover = TabPages[index];
                //Make sure there is a TabPage being dragged
                if (e.Data.GetDataPresent(typeof(T)))
                {
                    e.Effect = DragDropEffects.Move;
                    T tabDrag = (T)e.Data.GetData(typeof(T));
                    int dragIndex = TabPages.IndexOf(tabDrag);
                    int dropIndex = TabPages.IndexOf(tabHover);

                    if (dragIndex != dropIndex)
                    {
                        if ((dragIndex > dropIndex && pt.X < (GetTabRect(dropIndex).Left + GetTabRect(dragIndex).Width))
                            || (dragIndex < dropIndex && pt.X > (GetTabRect(dropIndex).Right - GetTabRect(dragIndex).Width))
                            )
                        {
                            TabPages.RemoveAt(dragIndex);
                            TabPages.Insert(dropIndex, tabDrag);
                            SelectedTab = tabDrag;
                        }
                    }
                }
                else
                {
                    e.Effect = DragDropEffects.None;
                }
            }
        }
    }
}
